#include <Program.hpp>

namespace Olorin
{
	namespace Graphics
	{
		Program::Program()
		{
			programID = glCreateProgram();
			compiled = false;
		}
		
		Program::~Program()
		{
			glDeleteProgram(programID);
		}
		
		const GLuint& Program::getProgramID() const
		{
			return programID;
		}
		
		const bool Program::attach(const Shader& shader)
		{
			glAttachShader(programID, shader.getShaderID());
			compiled = false;
			return true;
		}
		
		const bool Program::detach(const Shader& shader)
		{
			glDetachShader(programID, shader.getShaderID());
			compiled = false;
			return true;
		}
		
		const bool Program::compile()
		{
			int errCode = false;
			glLinkProgram(programID);
			glGetProgramiv(programID, GL_LINK_STATUS, &errCode);
			return compiled = errCode != 0;
		}
		
		const UniformAttribute Program::getUniformAttribute(const char* name)
		{
			return UniformAttribute(glGetUniformLocation(programID, name));
		}
		
		const VertexAttribute Program::getVertexAttribute(const char* name)
		{
			return VertexAttribute(glGetAttribLocation(programID, name));
		}
		
		void Program::begin()
		{
			if(!compiled)
				compile();
			glUseProgram(programID);
		}
			
		void Program::end()
		{
			glUseProgram(0);
		}
	}
}